import {reqGoodsInfo,reqAddOrUpdateShopCart} from '@/api';
//封装游客身份模块 随机生成一个字符串
import {getUUID} from '@/utils/uuid_taken'
const state = {
	goodInfo:{},
	//游客临时身份
	uuid_taken:getUUID()	
};
const mutations = {
	GETGOODINFO(state,goodInfo){
		state.goodInfo = goodInfo;
	},
};
const actions = {
	// 获取产品信息的action
	async getGoodInfo({commit},skuId){
		let result = await reqGoodsInfo(skuId);
		if(result.code === 200){ 
			commit('GETGOODINFO',result.data);
		}else{
			// 若获取失败，提示失败的原因
			alert(result.message)
		}
	},
	//将产品加入购物车
	async addOrUpdateShopCart({commit},{skuId,skuNum}){
		let result = await reqAddOrUpdateShopCart(skuId,skuNum);
		console.log(result)
		//服务器无回调,不需要存储Vuex中
		if(result.code === 200){
			return "ok"
		}else{
			return Promise.reject(new Error('faile'))
		}
	}
};
// 简化数据
const getters = {
	// 路径导航简化的数据
	categoryView(state){
		return state.goodInfo.categoryView||{};
	},
	// 产品信息简化的数据
	skuInfo(state){
		return state.goodInfo.skuInfo||{};
	},
	// 产品售卖属性的简化
	spuSaleAttrList(state){
		return state.goodInfo.spuSaleAttrList||[];
	}
};
export default{
	state,
	mutations,
	actions,
	getters
}